package com.linln.modules.homerotation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linln.common.utils.DateUtil;
import com.linln.common.utils.PathMappingUtil;
import com.linln.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.modules.homerotation.domain.HomeRotation;
import com.linln.modules.homerotation.mapper.HomeRotationMapper;
import com.linln.modules.homerotation.service.HomeRotationService;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.homerotation.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.homerotation.service.impl
 * @Description: TODO
 * @date Date : 2021年01月12日 11:08
 */

@Service
public class HomeRotationServiceImpl extends ServiceImpl<HomeRotationMapper, HomeRotation> implements HomeRotationService {

    @Autowired
    private HomeRotationMapper rotationMapper;

    @Override
    public List<HomeRotation> getHoneRotation(Integer rotationType) {
        return this.list(new QueryWrapper<HomeRotation>().eq("hr_type", rotationType).orderByAsc("ig_order"));

    }

    @Override
    public boolean addHoneRotation(MultipartFile file, HomeRotation homeRotation) {
        String path = PathMappingUtil.saveImg(PathMappingUtil.HOMEROTATIONPATH, PathMappingUtil.HOMEROTATIONMAPPING, file);
        homeRotation.setHrAddress(path);
        homeRotation.setHrCreateDate(DateUtil.formatDate(new Date()));
        return this.save(homeRotation);
    }

    @Override
    public boolean updateHoneRotation(MultipartFile file, HomeRotation homeRotation) {
        if (file.getOriginalFilename().equals("default")) {

        } else {
            HomeRotation byId = this.getById(homeRotation.getHrId());
            String delPath = PathMappingUtil.getPath(PathMappingUtil.HOMEROTATIONPATH, byId.getHrAddress());
            new File(delPath).delete();

            String path = PathMappingUtil.saveImg(PathMappingUtil.HOMEROTATIONPATH, PathMappingUtil.HOMEROTATIONMAPPING, file);
            homeRotation.setHrAddress(path);
        }
        return this.updateById(homeRotation);
    }

    @Override
    public boolean deleteHoneRotation(String id) {
        HomeRotation byId = this.getById(id);
        String delPath = PathMappingUtil.getPath(PathMappingUtil.HOMEROTATIONPATH, byId.getHrAddress());
        new File(delPath).delete();
        return this.removeById(id);
    }
}
